package androidx.gdata.spreadsheets.xml;


import org.w3c.dom.Node;

import java.util.List;

import androidx.gdata.Author;
import androidx.gdata.Category;
import androidx.gdata.Generator;
import androidx.gdata.LinkUrl;
import androidx.gdata.OpenSearch;
import androidx.gdata.spreadsheets.Spreadsheet;
import androidx.gdata.spreadsheets.SpreadsheetCollection;
import androidx.gdata.xml.AuthorDeserializer;
import androidx.gdata.xml.CategoryDeserializer;
import androidx.gdata.xml.GeneratorDeserializer;
import androidx.gdata.xml.ThingWithLinksDeserializer;

public class SpreadsheetCollectionDeserializer extends
  ThingWithLinksDeserializer<SpreadsheetCollection> {
  
  private static CategoryDeserializer categoryBuilder =
    new CategoryDeserializer();
  private static AuthorDeserializer authorBuilder =
    new AuthorDeserializer();
  private static GeneratorDeserializer generatorBuilder =
    new GeneratorDeserializer();
  private static SpreadsheetDeserializer spreadsheetEntryBuilder =
    new SpreadsheetDeserializer();

  @Override
  public SpreadsheetCollection build(Node feedNode) {
   node = feedNode;
   String id = getText("id");
   String updated = getText("updated");
   Category category = null;
   Node categoryNode = getChildNode("category");
   if (categoryNode != null) {
     category = categoryBuilder.build(categoryNode);
   }
   String title = getText("title");
   String totalResults = getText("openSearch:totalResults");
   String startIndex = getText("openSearch:startIndex");
   String itemsPerPage = getText("openSearch:itemsPerPage");
   OpenSearch openSearch = new OpenSearch(totalResults, startIndex,
       itemsPerPage);
   List<Node> entryNodes = getChildNodes("entry");
   Spreadsheet[] entries = new Spreadsheet[entryNodes.size()];
   int i = 0;
   for (Node entryNode : entryNodes) {
     Spreadsheet entry = spreadsheetEntryBuilder.build(entryNode);
     entries[i++] = entry;
   }
   LinkUrl[] links = getLinks();
   return new SpreadsheetCollection(id, updated, category, title, links,
       entries, openSearch);
   
  }

}
